package com.androidhuman.example.CreateActivity;

//import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.app.TimePickerDialog;
import android.widget.DatePicker;
import android.widget.TabHost;
import android.widget.TimePicker;
import android.widget.Toast;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;

import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

//TabActivity를 이용해서 클래스 구현
public class NewActivity extends TabActivity {
	/** Called when the TabActivity is first created. */

	private ProgressDialog progressDialog;	
	
	//옵션메뉴 구현시 필요한 상수 선언부분!
	private static final int MENU_ONE	= 0;
	private static final int MENU_TWO 	= 1;
	private static final int MENU_THREE = 2;
	private static final int MENU_FOUR	= 3;
	private static final int MENU_FIVE	= 4;
	private static final int MENU_SIX	= 5;
	private static final int MENU_SEVEN	= 6;
	private static final int MENU_EIGHT	= 7;
	
	private boolean isEnabled = true;
	//옵션메뉴 구현시 필요한 상수 선언부분 끝!
	
	private TabHost 	mTabHost;
	private Button 		finishActivityButton;
	
	private int 		LENGTH_TO_SHOW	= Toast.LENGTH_SHORT;
	private EditText	textToShow;
	private RadioGroup	radioGroup;
	private Button		showToastButton;
	
	private TextView	tvHello;
	private ImageView	ivIcon;
	private Button		btnHello;

	private Button		okcdBtn,ldBtn,pdBtn,scdBtn,mcdBtn,ccldBtn;
	
	private Button		datePickBtn,timePickBtn,callDialogBtn;	
	private TextView	mDateText,mTimeText;
	
	private Button		registerNotification;
	private EditText	tickerText,contentTitle,contentText;
	private NotificationManager nm;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		menu.add(0, MENU_ONE, 	Menu.NONE, "One").setIcon(android.R.drawable.ic_menu_add);
		menu.add(0, MENU_TWO, 	Menu.NONE, "Two").setIcon(android.R.drawable.ic_menu_agenda);
		menu.add(0, MENU_THREE, Menu.NONE, "Three").setIcon(android.R.drawable.ic_menu_call);
		menu.add(0, MENU_FOUR, 	Menu.NONE, "Four").setIcon(android.R.drawable.ic_menu_camera);
		menu.add(0, MENU_FIVE, 	Menu.NONE, "Five").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		
		menu.add(0, MENU_SIX, 	Menu.NONE, "Six").setAlphabeticShortcut('x');
		menu.add(0, MENU_SEVEN, Menu.NONE, "Seven").setAlphabeticShortcut('v');
		menu.add(0, MENU_EIGHT, Menu.NONE, "Eight").setAlphabeticShortcut('w');
		
		return true;
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		menu.getItem(MENU_ONE).setEnabled(isEnabled);
		return true;		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		switch(item.getItemId()) {
			case MENU_ONE	:
			case MENU_TWO	:
			case MENU_THREE	:
			case MENU_FOUR	:
			case MENU_FIVE	:
				Toast.makeText(this, "Menu Selected : " + item.getItemId(), Toast.LENGTH_SHORT).show();
				isEnabled = false;
				return true;
				
			case MENU_SIX	:
			case MENU_SEVEN	:
			case MENU_EIGHT	:
				Toast.makeText(this, "Menu Selected(More) : " + item.getItemId(), Toast.LENGTH_SHORT).show();
				isEnabled = true;
				return true;
		}
		return false;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
		
		if(v == tvHello) {
			// menu_edit.xml 에서 메뉴 불러오기.
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.menu_edit, menu);
			
			menu.setHeaderIcon(R.drawable.icon);
			menu.setHeaderTitle("Context for Button");
			
			return;
		}
		
		if(v == ivIcon ) {
			// menu_image.xml 에서 메뉴 불러오기.
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.menu_image, menu);
			
			menu.setHeaderIcon(R.drawable.icon);
			menu.setHeaderTitle("Context for ImageView");
			
			return;
		}
		
		if(v == btnHello) {
			// menu_button.xml 에서 메뉴 불러오기.
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.menu_button, menu);
			
			menu.setHeaderIcon(R.drawable.icon);
			menu.setHeaderTitle("Context for Button");
			
			// Button의 Caption에 따라 메뉴 바꾸기
			if(btnHello.getText().toString().compareTo("Hi!") == 0 ) {
				menu.findItem(R.id.mnuHi).setChecked(true);
			}else {
				menu.findItem(R.id.mnuBye).setChecked(true);
			}
			return;
		}
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {		
		switch(item.getItemId()) {
			case R.id.mnuHi :
				btnHello.setText("Hi!");
				return true;
			case R.id.mnuBye :
				btnHello.setText("Bye!");
				return true;
		}
		
		return false;
	}			
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.newlayout5);
	    
	    tvHello		= (TextView)findViewById(R.id.TVHello);
	    ivIcon		= (ImageView)findViewById(R.id.TVIcon);
	    btnHello	= (Button)findViewById(R.id.BtnHi);
	    
	    registerForContextMenu( tvHello );
	    registerForContextMenu( ivIcon );
	    registerForContextMenu( btnHello );
	    
        mTabHost = getTabHost();
        mTabHost.setup();
        
        mTabHost.addTab(mTabHost.newTabSpec("tab1")
        						.setContent(R.id.tabs1)
        						.setIndicator("Tab1"));        
        
        mTabHost.addTab(mTabHost.newTabSpec("tab2")
								.setContent(R.id.tabs2)
								.setIndicator("Tab2"));
        
        mTabHost.addTab(mTabHost.newTabSpec("tab3")
								.setContent(R.id.tabs3)
								.setIndicator("Tab3"));	    

        mTabHost.addTab(mTabHost.newTabSpec("tab4")
								.setContent(R.id.tabs4)
								.setIndicator("Tab4"));
        
        mTabHost.addTab(mTabHost.newTabSpec("tab5")
								.setContent(R.id.tabs5)
								.setIndicator("Tab5"));
    
        mTabHost.addTab(mTabHost.newTabSpec("tab6")
								.setContent(R.id.tabs6)
								.setIndicator("Tab6"));
        
	    finishActivityButton = (Button)findViewById(R.id.finishActivity);
	    finishActivityButton.setOnClickListener(new OnClickListener() {
	    	@Override
	    	public void onClick(View v) {
	    		Intent intent = new Intent();
	    		intent.putExtra("data_int", 10);
	    		intent.putExtra("data_string","Android");
	    		setResult(TabActivity.RESULT_OK, intent);
	    		finish();
	    	}
	    });
	    
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {      	
        	@Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
        	   	switch(checkedId) {
            		case R.id.radio_long:
            			LENGTH_TO_SHOW = Toast.LENGTH_LONG;
            			break;
            		case R.id.radio_short:
            			LENGTH_TO_SHOW = Toast.LENGTH_SHORT;
            			break;
            	}
            }
        });
        
        textToShow = (EditText)findViewById(R.id.TextInput);
        
        showToastButton = (Button)findViewById(R.id.ShowToast);
        showToastButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		Toast.makeText(NewActivity.this, textToShow.getText().toString(), LENGTH_TO_SHOW).show();
        	}
        });
        
        okcdBtn = (Button)findViewById(R.id.OKCD);
        okcdBtn.setOnClickListener(new OnClickListener() {
	    	@Override
	    	public void onClick(View v) {
	    		new AlertDialog.Builder(NewActivity.this)
	    			.setTitle("OK Cancel Dialog")
	    			.setMessage("OK / Cancel 다이얼로그입니다.")
	    			.setPositiveButton("OK",new DialogInterface.OnClickListener() {
	    				@Override
	    				public void onClick(DialogInterface dialog, int whice) {
	    					Toast.makeText(NewActivity.this, "OK", Toast.LENGTH_SHORT).show();
	    				}
	    			})	    			
	    			.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
	    				@Override
	    				public void onClick(DialogInterface dialog, int which) {
	    					Toast.makeText(NewActivity.this, "Cancel", Toast.LENGTH_SHORT).show();
	    				}
	    			}).create().show();
	    	}
        });	    

        ldBtn = (Button)findViewById(R.id.LD);
        ldBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		new AlertDialog.Builder(NewActivity.this)
        			.setTitle("List Dialog")
        			.setItems(R.array.lists2, new DialogInterface.OnClickListener() {
        				@Override
        				public void onClick(DialogInterface dialog, int which) {
        					Toast.makeText(NewActivity.this, (which+1)+"번재 항목을 선택하였습니다.", Toast.LENGTH_SHORT).show();
        				}
        			}).create().show();        			
        	}
        });
        
        pdBtn = (Button)findViewById(R.id.PD);
        pdBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				progressDialog = new ProgressDialog(NewActivity.this);
				progressDialog.setTitle("Progress Dialog");
				progressDialog.setIcon(android.R.drawable.ic_dialog_info);
				progressDialog.setMessage("잠시만 기다려 주시던지 말던지..ㅋㅋㅋㅋㅋ");
				progressDialog.show();
			}
        });
        
        scdBtn = (Button)findViewById(R.id.SCD);
        scdBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		new AlertDialog.Builder(NewActivity.this)
        			.setIcon(android.R.drawable.ic_dialog_info)
        			.setTitle("Single Choice")
        			.setSingleChoiceItems(R.array.lists2, 1, new DialogInterface.OnClickListener() {
        				@Override
        				public void onClick(DialogInterface dialog, int which) {
        					Toast.makeText(NewActivity.this, (which+1)+"번째 항목을 선택하였습니다.", Toast.LENGTH_SHORT).show();
        				}
        			}).create().show();
        	}
        });
        
        mcdBtn = (Button)findViewById(R.id.MCD);
        mcdBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		new AlertDialog.Builder(NewActivity.this)
    			.setIcon(android.R.drawable.ic_dialog_info)
    			.setTitle("Multiple Choice")
    			.setMultiChoiceItems(R.array.lists2, new boolean[]{false,true,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
    				@Override
    				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    					Toast.makeText(NewActivity.this,(which+1)+"번째 항목 : "+ isChecked, Toast.LENGTH_SHORT).show();
    				}
    			}).create().show();        		
        	}
        });
        
        ccldBtn = (Button)findViewById(R.id.CCLD);
        ccldBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		LayoutInflater inflater = getLayoutInflater();
        		final View customView = inflater.inflate(R.layout.custom_layout, null);
        		new AlertDialog.Builder(NewActivity.this)
        			.setTitle("Custom Layout")
        			.setView(customView)
        			.setPositiveButton("확인", new DialogInterface.OnClickListener() {
        				@Override
        				public void onClick(DialogInterface dialog, int which) {
        					EditText password = (EditText)customView.findViewById(R.id.password);
        					Toast.makeText(NewActivity.this, password.getText().toString(), Toast.LENGTH_LONG).show();
        				}
        			}).create().show();
        	}
        });
        
        datePickBtn 	= (Button)findViewById(R.id.datePickBtn);
        mDateText		= (TextView)findViewById(R.id.dateText);
        datePickBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		new DatePickerDialog(NewActivity.this, new DatePickerDialog.OnDateSetListener() {					
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
						// TODO Auto-generated method stub
						mDateText.setText(year + "년" + (monthOfYear+1) + "월" + dayOfMonth + "일");
					}
				},2010,0,15).show();
        	}
        });
        
        timePickBtn 	= (Button)findViewById(R.id.timePickBtn);
    	mTimeText		= (TextView)findViewById(R.id.timeText);
    	timePickBtn.setOnClickListener(new OnClickListener() {
    		@Override
    		public void onClick(View v) {
    			new TimePickerDialog(NewActivity.this, new TimePickerDialog.OnTimeSetListener() {					
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						// TODO Auto-generated method stub
						mTimeText.setText(hourOfDay + "시" + minute + "분");
					}
				},10,30,false).show();
    		}
    	});
    	
        callDialogBtn	= (Button)findViewById(R.id.callDialogActivity);
        callDialogBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		startActivity(new Intent(NewActivity.this, DialogActivity.class));
        	}
        });
        
        registerNotification = (Button)findViewById(R.id.registerNotification);
        tickerText		= (EditText)findViewById(R.id.tickerText);
        contentTitle	= (EditText)findViewById(R.id.contentTitle);
        contentText		= (EditText)findViewById(R.id.contentText);
        registerNotification.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		//Get Notification Service
        		nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        		
        		PendingIntent intent = PendingIntent.getActivity(NewActivity.this, 0, new Intent(NewActivity.this,NotificationMessage.class),0);
        		        		
        		String ticker	= tickerText.getText().toString();
        		String title	= contentTitle.getText().toString();
        		String text		= contentText.getText().toString();
        		
        		//Create Notification Object
        		Notification notification = new Notification(android.R.drawable.ic_input_add,
        													 ticker,
        													 System.currentTimeMillis());
        		
        		notification.setLatestEventInfo(NewActivity.this, title, text, intent);
        		
        		nm.notify(1234, notification);
        		Toast.makeText(NewActivity.this, "Notification Registered.", Toast.LENGTH_SHORT).show();       		
        	}
        });
	}
}

//기본 Activity를 상속해서 구현한 클래스.
/*
public class NewActivity extends Activity {

	/** Called when the activity is first created. */
	
//	private Button finishActivityButton;
/*	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    //setContentView(R.layout.newlayout);
	    //setContentView(R.layout.newlayout2);
	    //setContentView(R.layout.newlayout3);
	    //setContentView(R.layout.newlayout4);
	    setContentView(R.layout.newlayout5);
	    
	    finishActivityButton = (Button)findViewById(R.id.finishActivity);
	    finishActivityButton.setOnClickListener(new OnClickListener() {
	    	@Override
	    	public void onClick(View v) {
	    		Intent intent = new Intent();
	    		intent.putExtra("data_int", 10);
	    		intent.putExtra("data_string","Android");
	    		setResult(Activity.RESULT_OK, intent);
	    		finish();
	    	}
	    });
	    // TODO Auto-generated method stub
	}
} */
